package cn.yjr.vector;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.yjr.vector.tools.MD5;
import cn.yjr.vector.tools.SharedPreferencesHelper;
import cn.yjr.vector.tools.YJRHttpClient;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

public class SetPwdActivity extends SherlockActivity implements OnClickListener{

	LayoutInflater inflater;
	ActionBar actionBar;
	View title_bar;
	TextView set_pwd_head_tv, set_pwd_tv;
	EditText set_pwd_1, set_pwd_2;
	Context context;
	String response;
	JSONObject jsonInfo;
	String new_pwd;
	String phone, pwd;
	static final int LOGIN_MSG = 0;
	SharedPreferencesHelper spHelper;
	static String login_url = "http://115.28.222.1/yijiaren/user/login.action";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_pwd);
		inflater = this.getLayoutInflater();
		actionBar = getSupportActionBar();
		context = this;
		this.spHelper = new SharedPreferencesHelper(this.context);
		phone = spHelper.getStringValue("phone");
		pwd = spHelper.getStringValue("password");
		initTitleBar();
		
		initComponent();
		
		initEventListener();
		
		
		
	
	}
	
	private void initTitleBar(){
		ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.WRAP_CONTENT,Gravity.CENTER);
		title_bar = inflater.inflate(R.layout.set_pwd_action_bar, null);
		
		actionBar.setCustomView(title_bar,lp);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);
	}
	private void initComponent(){
		set_pwd_1 = (EditText) findViewById(R.id.set_pwd_1);
		set_pwd_2 = (EditText) findViewById(R.id.set_pwd_2);
		set_pwd_head_tv = (TextView) findViewById(R.id.set_pwd_head_tv);
		set_pwd_tv = (TextView) findViewById(R.id.set_pwd_tv);
		set_pwd_head_tv.setText("注册手机号：" + phone);
		
		
	}
	
	private void initEventListener(){
		set_pwd_tv.setOnClickListener(this);

		
	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.set_pwd_tv:
			String pwd1 = set_pwd_1.getText().toString();
			String pwd2 = set_pwd_2.getText().toString();
			if(checkPwd(pwd1,pwd2)){
				new_pwd = MD5.getMD5(pwd1);
				Intent intent = new Intent(SetPwdActivity.this, CheckCaptchaActivity.class);
				intent.putExtra("new_pwd", new_pwd);
				startActivity(intent);
			}
			break;
		default:
				break;
		}
		
	}
	
	

	
	
	private boolean checkPwd(String pwd1, String pwd2){
		if(pwd1==null||pwd2==null||pwd1.equals("")||pwd2.equals(""))
		{
			Toast.makeText(context, "密码不能为空，请输入后再试", Toast.LENGTH_SHORT).show();
		}else if(!pwd1.equals(pwd2)){
			Toast.makeText(context, "两次密码不一样，请重新输入", Toast.LENGTH_SHORT).show();
		}else{
			return true;
		}
		return false;
	}

}